<?php
require_once 'models/client.inc';
require_once 'models/catalog.inc';

class TMSOrderState extends TEnumeration {
	const INBOX = 0;
	const INWORK = 1;
	const PAID = 2;
	const CANCELED = 3;
}

/**
 * @author dkrasilnikov@gmail.com
 * @orm.table delivery_type(code)
 * @orm.identity explicit
 * @orm.key Code 
 */
class TMSDeliveryMethod extends TNOrmObject {
/**
 * @var string
 * @orm.scalar code
 */	
	public $Code;
/**
 * @var string
 * @orm.scalar name
 */	
	public $Name;
/**
 * @var string
 * @orm.scalar component
 */	
	public $Component;
	
/**
 * @var bool
 * @orm.scalar active
 */	
	public $IsActive;
	
/**
 * @var int
 * @orm.scalar order
 */	
	public $OrderNumber;
	
/**
 * @var string
 * @orm.scalar setup
 */	
	public $Setup;
	
	private $_setup_;
	
	public function __toString(){
		return $this->Name;
	}
	
	public function __set($nm, $value){
		switch ($nm){
			case 'SetupData':{
				if (is_array($value)){
					$this->_setup_ = $value;
					$this->Setup = json_encode($this->_setup_);
				}
			}break;
			default:{
				if (isset($this->SetupData[$nm])){
					$this->_setup_[$nm]->value = $value;
					$this->Setup = json_encode($this->_setup_);
				} else	
					return parent::__get($nm);
			}break;
		}
	}
	
	public function __get($nm){
		switch ($nm){
			case 'SetupData':{
				if (is_null($this->_setup_)){
					if ($this->Setup)
						$this->_setup_ = (array)json_decode($this->Setup);
					else
						$this->_setup_ = array();
				}
				return $this->_setup_;
			}break;
			default:{
				if (isset($this->SetupData[$nm]))
					return $this->_setup_[$nm]->value;
				return parent::__get($nm);
			}break;
		}
	}
}

/**
 * @author dkrasilnikov@gmail.com
 * @orm.table order(orderid)
 * @orm.key Id 
 * 
 * @property TMSClient $Client {@orm.reference Id(client)}
 * @property TMSUser $Executor {@orm.reference Id(executor)}
 * @property int $State {@orm.scalar state}
 * @property TMSDeliveryMethod $Delivery {@orm.reference Code(delivery)}
 * @property TMSOrderPosition $Positions {@orm.collection Order(orderid)}
 * @property string $Customer
 * @property float $Amount;
 * @property int $Quantaty;
 */
class TMSOrder extends TNOrmObject {
/**
 * @var int
 * @orm.scalar orderid
 */	
	public $Id;
/**
 * @var TMSClient
 */
	protected $_orm_client_;
/**
 * @var string
 * @orm.scalar guest_name
 */	
	public $GuestName;
/**
 * @var string
 * @orm.scalar guest_phone
 */	
	public $GuestPhone;
/**
 * @var string
 * @orm.scalar guest_email
 */	
	public $GuestEmail;
	
/**
 * @var string
 * @orm.scalar guest_address
 */	
	public $GuestAddress;
	
/**
 * @var date
 * @orm.scalar added
 */	
	public $Added;
	
/**
 * @var string
 * @orm.scalar order_comment
 */	
	public $Comment;
	
	
/**
 * @var int
 */	
	protected $_orm_state_;
	
	protected $_orm_positions_;
	
	private $_amount_;
	private $_quantaty_;
	
/**
 * @var TMSUser
 */	
	protected $_orm_executor_;
	
/**
 * @var TMSDeliveryMethod
 */
	protected $_orm_delivery_;
	
	public function __set($nm,$value){
		if ($nm == 'State'){
			if (is_numeric($value))
				$value = new TMSOrderState((int)$value);
			if (!($value instanceof TMSOrderState))
				throw new TCoreException(TCoreException::ERR_BAD_VALUE);
			$value = (string)$value;	
		}
		parent::__set($nm,$value);
	}
	
	private function _check_positions(){
		if (is_null($this->_amount_) || is_null($this->_quantaty_)){
			$this->_amount_ = 0;
			$this->_quantaty_ = 0;
			foreach ($this->Positions as $p){
				$this->_quantaty_ += $p->Quantaty;
				$this->_amount_ += $p->Quantaty * $p->OrderPrice;
			}
		}
	}
	
	public function __get($nm){
		switch ($nm){
			case 'Customer':return is_null($this->Client)?($this->GuestName.'['.$this->GuestPhone.']'):($this->Client->Name.' '.$this->Client->Patronymic.'['.$this->Client->Mobile.']');break;
			case 'Amount':{$this->_check_positions();return $this->_amount_;}break;
			case 'Quantaty':{$this->_check_positions();return $this->_quantaty_;}break;
			default:return parent::__get($nm);break;
		}
	}
	
	public function __toString(){
		return $this->Id.' ['.$this->Added.'] '.$this->Customer.' : '.$this->Amount.'/'.$this->Quantaty;
	}
}

class TMSCustomerChoice extends TNOrmObject {
	/**
	 * @var string
	 */
	protected $_orm_data_hash_;
	
	/**
	 * @var string
	 */
	protected $_orm_data_;
	
	
	private $_choice_ = array();
	
	public function __set($nm,$value){
		switch ($nm){
			case 'Data':{
				if (!$value){
					$this->DataHash = '';
					$this->_choice_ = array();
					parent::__set($nm,null);
				} else {
					if (!is_string($value))
						throw new TCoreException(TCoreException::ERR_BAD_VALUE);
					$this->DataHash = THash::Apply($value,THash::MD5);
					$this->_choice_ = json_decode($value,true);
					parent::__set($nm,$value);
				}
			}break;
			case 'DataHash':$this->_orm_data_hash_ = is_null($value)?'':$value;
			default:parent::__set($nm,$value);break;
		}
	}
	
	public function __get($nm){
		$v = parent::__get($nm);
		if (!$v){
			if (isset($this->_choice_[$nm]))
				return $this->_choice_[$nm];
		}
		return $v;
	}
	
	public function Options(){return $this->_choice_;}
	
	public function SetOption($key,$value){
		$this->_choice_[$key] = $value;
		$this->Data = json_encode($this->_choice_);
	}
}



/**
 * @author dkrasilnikov@gmail.com
 * @orm.table order_position(order,article,data_hash)
 * @orm.key Order
 * @orm.key Product
 * @orm.key DataHash
 * 
 * @property TMSOrder $Order {@orm.reference Id(order)}
 * @property TMSArticle $Product {@orm.reference Id(article)}
 * @property string $Data {@orm.scalar data}
 * @property string $DataHash {@orm.scalar data_hash}
 */
class TMSOrderPosition extends TMSCustomerChoice {
/**
 * @var TMSOrder
 */	
	protected $_orm_order_;
/**
 * @var TMSArticle
 */	
	protected $_orm_product_;
		
/**
 * @var int
 * @orm.scalar quantaty
 */
	public $Quantaty;
	
/**
 * @var float
 * @orm.scalar order_price
 */
	public $OrderPrice;
	
/**
 * @var string
 * @orm.scalar applied_discounts
 */
	public $AppliedDiscounts;
	
	public function __toString(){
		return (string)$this->Product;
	}		
}

/**
 * @author dkrasilnikov@gmail.com
 * @orm.table cart_position(client,article,data_hash)
 * @orm.key Client
 * @orm.key Product
 * @orm.key DataHash
 * 
 * @property TMSClient $Client {@orm.reference Id(client)}
 * @property TMSArticle $Product {@orm.reference Id(article)}
 * @property string $Data {@orm.scalar data}
 * @property string $DataHash {@orm.scalar data_hash}
 */
class TMSCartPosition extends TMSCustomerChoice {
/**
 * @var TMSClient
 */	
	protected $_orm_client_;

/**
 * @var TMSArticle
 */	
	protected $_orm_product_;

/**
 * @var string
 */
	protected $_orm_data_hash_;
	
/**
 * @var string
 */
	protected $_orm_data_;
	
/**
 * @var int
 * @orm.scalar quantaty
 */
	public $Quantaty;

/**
 * @var bool
 * @orm.scalar bookmarked
 */
	public $Bookmarked;
			
	public function __toString(){
		return (string)$this->Product;
	}
}